<template>
  <div class="lix-container lix-add__bg">
    <div class="lix-add__listwrap">
      <div class="lix-add__list">
        <div class="lix-add__item" v-for="(item, idx) in list" :key="idx">
          <div class="left" @click="onClick" :data-item="item">
            <div class="top">
              <div class="name">{{ item.name }}</div>
              <div class="tel">{{ item.tel }}</div>
              <div class="default" v-if="item.isDefault">[默认]</div>
            </div>
            <div class="bottom">
              <div class="detail">
                <p>{{ item.province }}{{ item.city }}{{ item.country }}</p>
                <p>{{ item.addressDetail }}</p>
              </div>
            </div>
          </div>
          <div class="right">
            <div class="edit">
              <img src="../../static/images/edit-2.png" alt="" @click="editAddress" :data-item="item">
              <img src="../../static/images/delete2.png" alt="" @click="deleteAddress" :data-id="item.id">
            </div>
          </div>
        </div>
      </div>
    </div>
    <Abnor type="DATA" title="您还没有添加地址" button="新增收货地址" @abnortap="addAddress" v-if="list.length < 1"/>
    <div class="lix-add__btn" v-if="list.length > 0">
      <button @click="addAddress">新增收货地址</button>
    </div>
  </div>
</template>

<script>
import Abnor from '../components/abnor/abnor';
import { getAddressList, deleteAddressApi } from '../api';
export default {
  components: {
    Abnor
  },
  data: {
    list: [],
    type: ''
  },
  onShow() {
    this.getAddress();
  },
  onLoad(option) {
    console.log(option);
    if (option.type) {
      this.type = option.type;
    }
  },
  methods: {
    getAddress() {
      getAddressList()
        .then(res => {
          console.log(res);
          if (res.data.length > 0) {
            this.list = res.data;
          } else {
            this.list = [];
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    addAddress(e) {
      console.log('新增地址');
      wx.navigateTo({
        url: '/pages/addressEdit?isedit=0'
      });
    },
    editAddress(e) {
      const item = e.mp.currentTarget.dataset.item;
      console.log(item);
      wx.setStorageSync('UPDATE_ADDRESS', item);
      wx.navigateTo({
        url: '/pages/addressEdit?isedit=1'
      });
    },
    deleteAddress(e) {
      const id = e.mp.currentTarget.dataset.id;
      deleteAddressApi(id)
        .then(res => {
          console.log(res);
          this.getAddress();
        })
        .catch(e => {
          console.log(e);
        });
    },
    onClick(e) {
      if (this.type !== 'Chose') {
        return;
      }
      console.log(e);
      const item = e.mp.currentTarget.dataset.item;
      console.log('item 触摸事件');
      wx.setStorageSync('SELECTED_ADDRESS', item);
      wx.navigateBack();
    }
  }
};
</script>

<style lang="scss">
@import "../theme-chalk/mixins/mixins.scss";
@import "../theme-chalk/common/var.scss";
@import "../theme-chalk/_main.scss";

@include b(add) {
  @include e(list) {
    display: flex;
    flex-direction: column;
  }

  @include e(bg) {
    background: #f5f5f5;
  }

  @include e(list) {
    border-top: 1px solid #e0e0e0;
  }

  @include e(item) {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    height: 84px;
    border-bottom: 1px solid #e0e0e0;
    background: #fff;

    .left {
      display: flex;
      flex-direction: column;
      margin-left: 33px;
      width: 60%;

      .top {
        display: flex;
        flex-direction: row;
        align-items: center;
        font-size: 15px;
        color: #474747;
        font-weight: 400;
        .name, .default {
          color: #ff6b00;
        }
        .tel {
          margin: 0 10px 0 39px;
        }
      }
      .detail {
        font-size: 13px;
      }
    }

    .right {
      margin-right: 12px;
      .edit {
        img {
          width: 20px;
          height: 20px;
          margin-right: 10px;
        }
      }
    }
  }
  @include e(btn) {
    button {
      margin: 30px 12px 0 12px;
      background: #ff6b00;
      color: #fff;
      height: 44px;
      font-size: 13px;
      line-height: 44px;
      border-radius: 0;
      border: none;
    }
  }
}
</style>
